route.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { NextRequest, NextResponse } from 'next/server';
  2. import { ResultDto } from '@/types/response/common';
  3. import { fetchJson } from '@/lib/utils/server';
  4. export async function GET(request: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
  5. const { path } = await params;
  6. const endpoint = `/api/donation/${path.join('/')}`;
  7. const url = new URL(request.url);
  8. const res: ResultDto = await fetchJson(`${endpoint}${url.search}`, {
  9. method: 'GET'
  10. });
  11. return NextResponse.json(res);
  12. }
  13. export async function POST(request: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
  14. const { path } = await params;
  15. const endpoint = `/api/donation/${path.join('/')}`;
  16. const contentType = request.headers.get('content-type') || '';
  17. if (contentType.includes('multipart/form-data')) {
  18. const res: ResultDto = await fetchJson(endpoint, {
  19. method: 'POST', body: await request.arrayBuffer(),
  20. headers: { 'Content-Type': contentType }
  21. });
  22. return NextResponse.json(res);
  23. }
  24. const res: ResultDto = await fetchJson(endpoint, { method: 'POST', body: JSON.stringify(await request.json()) });
  25. return NextResponse.json(res);
  26. }
  27. export async function DELETE(_request: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
  28. const { path } = await params;
  29. const endpoint = `/api/donation/${path.join('/')}`;
  30. const res: ResultDto = await fetchJson(endpoint, {
  31. method: 'DELETE'
  32. });
  33. return NextResponse.json(res);
  34. }